package org.larkdoc.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.larkdoc.dao.DocInfoMapper;
import org.larkdoc.dao.DocReleaseMapper;
import org.larkdoc.model.entity.DocInfo;
import org.larkdoc.model.entity.DocRelease;
import org.larkdoc.service.DocReleaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文档版本业务功能实现类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Service("docReleaseService")
public class DocReleaseServiceImpl implements DocReleaseService {
    
    private final static Logger log = LoggerFactory.getLogger(DocReleaseServiceImpl.class);
    
    @Autowired
    DocReleaseMapper            docReleaseMapper;
    
    @Autowired
    DocInfoMapper               docInfoMapper;
    
    @Override
    public int insert(DocRelease record) {
        return docReleaseMapper.insert(record);
    }
    
    @Override
    public int insertSelective(DocRelease record) {
        return docReleaseMapper.insertSelective(record);
    }
    
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int versionRelease(DocRelease record) {
        if (null == record) {
            log.info("method versionRelease param error");
            return 0;
        }
        
        if (docReleaseMapper.checkExists(record) > 0) {
            log.info("method versionRelease docRelease is Existed.docId:{},versionNo:{}", record.getDocId(),
                record.getVersionNo());
            return 0;
        }
        // DocInfo docInfo = docInfoMapper.selectByPrimaryKey(record.getDocId());
        int result = insert(record);
        if (result < 1) {
            return result;
        }
        DocInfo updDocInfo = new DocInfo();
        updDocInfo.setId(record.getDocId());
        updDocInfo.setLatestVersion(record.getVersionNo());
        updDocInfo.setUpdateTime(new Date());
        return docInfoMapper.updateByPrimaryKeySelective(updDocInfo);
        
    }
    
    /**
     * @param docInfo
     * @param record
     * @return [参数说明]
     * 
     * @return DocRelease [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("unused")
    private DocRelease combDocRelease(DocInfo docInfo, DocRelease record) {
        DocRelease docRelease = new DocRelease();
        
        docRelease.setDocId(record.getDocId());
        docRelease.setName(docInfo.getName());
        docRelease.setTag(docInfo.getTag());
        docRelease.setVersionNo(record.getVersionNo());
        docRelease.setDescription(record.getDescription());
        docRelease.setVisibleRange(docInfo.getVisibleRange());
        docRelease.setMdDetail(docInfo.getMdDetail());
        docRelease.setHtmlDetail(docInfo.getHtmlDetail());
        docRelease.setCatalog(docInfo.getCatalog());
        docRelease.setPath(docInfo.getPath());
        docRelease.setShortUrl(docInfo.getShortUrl());
        docRelease.setCreator(docInfo.getCreator());
        docRelease.setCreateTime(docInfo.getCreateTime());
        docRelease.setRemark(docInfo.getRemark());
        
        return docRelease;
    }
    
    @Override
    public boolean checkExists(DocRelease record) {
        return docReleaseMapper.checkExists(record) > 0 ? true : false;
    }
    
    @Override
    public List<DocRelease> getDocReleasesByDocId(Integer docId, String account) {
        if (null == docId || StringUtils.isBlank(account)) {
            log.info("method getDocReleasesByDocId param error");
            return null;
        }
        return docReleaseMapper.getDocReleasesByDocId(docId, account);
    }
    
    @Override
    public DocRelease getDocReleasesByDocIdAndVersion(Integer docId, String versionNo) {
        if (null == docId || StringUtils.isBlank(versionNo)) {
            log.info("method getDocReleasesByDocId param error");
            return null;
        }
        return docReleaseMapper.getDocReleasesByDocIdAndVersion(docId, versionNo);
    }
    
    @Override
    public List<DocRelease> getHistory(Integer docId) {
        return docReleaseMapper.getHistory(docId);
    }
    
}
